package j2024.j202408;

import java.util.ArrayList;
import java.util.List;

public class j0810 {
    int left,right,nn;
    StringBuilder ppath;
    List<String> rett;
    public List<String> generateParenthesis(int n) {
        nn  = n;
        ppath = new StringBuilder();
        rett = new ArrayList<>();
        ddfs();
        return rett;
    }
    public void ddfs(){
        if(right==nn){
            rett.add(ppath.toString());
        }
        if(left<nn){
            ppath.append('(');
            left++;
            ddfs();
            ppath.deleteCharAt(ppath.length()-1);
            left--;
        }
        if(right<left){
            ppath.append(')');
            right++;
            ddfs();
            ppath.deleteCharAt(ppath.length()-1);
            right--;
        }
    }
}
